package org.stone.project.dynamicDataSource.config;

import java.util.Map;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.stone.project.dynamicDataSource.utils.DynamicDataSourceContextHolder;

/**
 * 动态数据源
 */
public class DynamicDataSource extends AbstractRoutingDataSource
{
    public DynamicDataSource(String defaultDataSourceName,Map<Object, Object> dsMap)
    {
        if (dsMap.size() == 0)
            throw new RuntimeException("数据源为空");
        DynamicDataSourceContextHolder.setDefaultDsName(defaultDataSourceName);
        super.setTargetDataSources(dsMap);
        super.afterPropertiesSet();
    }

    @Override
    protected Object determineCurrentLookupKey()
    {
        return DynamicDataSourceContextHolder.getDataSourceTypeOrDefault();
    }
}